﻿/*                       NOTICE
 * 
 * This software was produced for the U. S. Government
 * under Contract No. FA8721-10-C-0001, and is
 * subject to the Rights in Noncommercial Computer Software
 * and Noncommercial Computer Software Documentation Clause
 * (DFARS) 252.227-7014 (JUN 1995)
 * 
 * Approved for Public Release: 11-2435
 * © 2011 The MITRE Corporation. All Rights Reserved. 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;


namespace MXSE.MultiTouch.Gestures
{
    public class BoxingGesture : ComplexGesture
    {
        private bool updated = false;

        public BoundingBox box { get; private set; }

        public BoxingGesture(TouchGesture[] touches)
        {
            if (touches.Length != 4)
                throw new Exception("BoxingGesture requires exactly four TouchGestures.");

            for (int i = 0; i < 4; i++)
            {
                AddBasic(touches[i]);
            }

            createBoundingBox();
        }

        //Want this to encompass all the touches
        public void createBoundingBox()
        {
            Vector3[] pos = new Vector3[4];
            for (int i = 0; i < 4; i++)
            {
                pos[i] = new Vector3(Basics[i].startPosition.X,Basics[i].startPosition.Y,-5);
            }

            float xMin, xMax, yMin, yMax;
            xMin = xMax = pos[0].X;
            yMin = yMax = pos[0].Y;
            for (int i = 1; i < 4; i++)
            {
                if (pos[i].X < xMin)
                    xMin = pos[i].X;
                if (pos[i].X > xMax)
                    xMax = pos[i].X;
                if (pos[i].Y < yMin)
                    yMin = pos[i].Y;
                if (pos[i].Y > yMax)
                    yMax = pos[i].Y;
            }

            Vector3 min = new Vector3(xMin, yMin, -5);
            Vector3 max = new Vector3(xMax, yMax, 5);
            box = new BoundingBox(min, max);
        }

        public override void Update()
        {
            if (!updated)
            {
                updated = true;
                base.Update();
            }
        }

        public override void OnDeath()
        {
            foreach (BasicGesture b in Basics)
                b.Parent = null;
        }

        public override void Start()
        {
            throw new NotImplementedException();
        }
    }
}
